package com.github.binarywang.demo.wechat.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.binarywang.demo.wechat.model.ResponseResult;
import com.github.binarywang.demo.wechat.model.User;
import com.github.binarywang.demo.wechat.service.WxUserService;
import com.github.binarywang.demo.wechat.utils.JsonUtils;

@RequestMapping({ "wechat/user" })
@RestController
public class WxUserController {
	@Autowired
	private WxUserService wxUserService;

	@GetMapping({ "list" })
	public String list() {
		return JsonUtils.toJson(this.wxUserService.list());
	}

	@GetMapping({ "delete" })
	public int delete(String openId) {
		return this.wxUserService.delete(openId);
	}

	@GetMapping({ "edit" })
	public int edit(User user) {
		return this.wxUserService.edit(user);
	}
	
	@GetMapping({ "list_" })
	public ResponseResult<User> list_(String content) {
		ResponseResult<User> result = new  ResponseResult<User>();
		try {
			List<User> userList = this.wxUserService.list_(content);
			result.setCode(0);
			result.setCount(userList.size());
			result.setData(userList);
			result.setMsg("查询成功");
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
}
